<html>
<head><meta charset="utf-8"><title>hope to improve Option::clone codegen · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html">hope to improve Option::clone codegen</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204630298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204630298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ridiculous_fish <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204630298">(Jul 22 2020 at 04:53)</a>:</h4>
<p>Hi all! I am a brand new rustc contributor, I hope to to improve the codegen for <code>Option::clone()</code> (current example: <a href="https://rust.godbolt.org/z/KTcTEo">https://rust.godbolt.org/z/KTcTEo</a>). My idea is that <code>Option::&lt;T&gt;.clone()</code> should be a memcpy if T is "trivial" and "small" in some sense. I would appreciate guidance for how to approach this fix - where it should go, the proper notion of "trivial", etc.</p>



<a name="204630876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204630876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204630876">(Jul 22 2020 at 05:08)</a>:</h4>
<blockquote>
<p>trivial and small</p>
</blockquote>
<p>That sounds like <code>Copy</code>.</p>



<a name="204630956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204630956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ridiculous_fish <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204630956">(Jul 22 2020 at 05:10)</a>:</h4>
<p>Not all "trivial" and "small" types are <code>Copy</code>, for example <code>Range</code> is not</p>



<a name="204631042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204631042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ridiculous_fish <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204631042">(Jul 22 2020 at 05:14)</a>:</h4>
<p>Actually maybe that's a better starting point? Can I fix the <code>Option&lt;Range&gt;</code> codegen? <a href="https://rust.godbolt.org/z/7a5Yqx">https://rust.godbolt.org/z/7a5Yqx</a></p>



<a name="204655531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204655531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204655531">(Jul 22 2020 at 11:53)</a>:</h4>
<p>This happens even for <code>Copy</code> types, <a href="https://rust.godbolt.org/z/xWYzTc">https://rust.godbolt.org/z/xWYzTc</a>. There are multiple avenues that could be attempted – generating <code>Clone</code> impl that does not <code>match</code> is one. The problem is its already something that is supposed to exist! (<a href="https://github.com/rust-lang/rust/pull/31414">https://github.com/rust-lang/rust/pull/31414</a>)</p>



<a name="204655571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204655571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204655571">(Jul 22 2020 at 11:53)</a>:</h4>
<p><code>impl&lt;T: Clone&gt; Clone for Option&lt;T&gt;</code> is manual in stdlib, rather than a derive, but even making a custom <code>Option</code> doesn’t help: <a href="https://rust.godbolt.org/z/GMTPnE">https://rust.godbolt.org/z/GMTPnE</a></p>



<a name="204655652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204655652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204655652">(Jul 22 2020 at 11:54)</a>:</h4>
<p>so I would probably start by looking into why the <code>Clone = Copy</code> for <code>T: Copy</code> from <a href="https://github.com/rust-lang/rust/issues/31414">#31414</a> is not kicking in.</p>



<a name="204655703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204655703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204655703">(Jul 22 2020 at 11:54)</a>:</h4>
<p>Isn't this because range is not <code>Copy</code>?</p>



<a name="204655834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204655834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204655834">(Jul 22 2020 at 11:56)</a>:</h4>
<p>the examples I’ve linked are Copy, there's even an assert there.</p>



<a name="204655861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204655861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204655861">(Jul 22 2020 at 11:56)</a>:</h4>
<p>Looking into those first would make sense to me.</p>



<a name="204656014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204656014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204656014">(Jul 22 2020 at 11:58)</a>:</h4>
<p>Could codegen know that a struct <em>could</em> be Copy (but isn’t for semantic reasons)and leverage that knowledge?</p>



<a name="204656049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204656049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204656049">(Jul 22 2020 at 11:58)</a>:</h4>
<p>Fixing a non-copy version is significantly more difficult, especially because you can’t always be sure that copying the whole thing is going to be cheaper than copying just a branch of it.</p>



<a name="204656070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204656070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204656070">(Jul 22 2020 at 11:59)</a>:</h4>
<p>think something along the lines of <code>#[derive(Clone)] enum { Cheap, StillCheap(u32), Expensive([u32; 1000]) }</code> // assume huge arrays implement clone, etc.</p>



<a name="204658686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204658686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204658686">(Jul 22 2020 at 12:29)</a>:</h4>
<p>large arrays are already special-cased as Copy/Clone in the compiler</p>



<a name="204658709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204658709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204658709">(Jul 22 2020 at 12:29)</a>:</h4>
<p>(at least according to the docs, <a href="https://doc.rust-lang.org/nightly/std/primitive.array.html">https://doc.rust-lang.org/nightly/std/primitive.array.html</a>)</p>



<a name="204758204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/hope%20to%20improve%20Option%3A%3Aclone%20codegen/near/204758204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ridiculous_fish <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/hope.20to.20improve.20Option.3A.3Aclone.20codegen.html#204758204">(Jul 23 2020 at 03:53)</a>:</h4>
<p>Thank you all for the starting points, I will see what I can do!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>